<template>
  <div class="container">
    <picker mode="date" @change="bindDateChange" fields="month" :value="date">
      <view class="picker choose">{{date}}</view>
    </picker>
    <div class="customer">
      <div class="left">
        <span class="customer_pic">
          <img
            :src="guest_info.headurl !== '0' ? guest_info.headurl : '/static/imgs/system_user.png'"
          >
        </span>
      </div>
      <div class="right flex-item">
        <span class="customer_name">
          <strong>{{guest_info.name}}</strong>
          <i v-if="guest_info.lv !== '' && guest_info.lv && guest_info.lv != 0 ">{{guest_info.lv}}类</i>
        </span>
        <span class="customer_phone">手机号 {{guest_info.phone}}</span>
        <span class="customer_arrivetime">最近到店时间: {{guest_info.arrivetime}}</span>
        <span class="customer_shop">
          <i></i>
          {{guest_info.shopname}}
        </span>
      </div>
    </div>
    <div class="wrap_box">
      <div class="title">
        <i class="shu"></i>
        <span>{{year}}年度客户整体规划</span>
      </div>
      <div class="body">
        <div class="flex">
          <span class="flex-item">规划业绩</span>
          <span v-if="yearfeat == ''">
            <input type="number" @blur="setfeat2" placeholder="未规划">
          </span>
          <span v-else class="flex">
            <input
              type="number"
              @blur="setfeat"
              :value="yearfeat[0].feat"
              :data-oldval="yearfeat[0].feat"
            >
            <span>元</span>
          </span>
        </div>
      </div>
    </div>
    <div class="wrap_box">
      <div class="title">
        <i class="shu"></i>
        <span>{{year}}年{{month}}月规划详情</span>
      </div>
      <div v-if="planlist.length === 0" class="none">
        <span>没有{{year}}年{{month}}的规划</span>
      </div>
      <div v-else class="body">
        <div
          style="padding-top: 8rpx;"
          @tap="showdetail(item.id)"
          v-for="(item,index) in planlist"
          :key="index"
        >
          <div class="plan_customer">
            <div class="left" style="margin-right: 16rpx;">
              <span class="customer_pic">
                <img :src="item.headurl || '/static/imgs/system_user.png'">
              </span>
            </div>
            <div class="right flex-item">
              <div style="display: flex;padding-top: 12rpx;justify-content: space-between;">
                <span>{{item.name}}对客户做了规划</span>
                <span style="font-size: 32rpx; font-weight: bold;">查看</span>
              </div>
              <div>
                <span>规划到店: {{item.arrivetime}}</span>
              </div>
            </div>
          </div>
          <div class="plan_yeji flex">
            <span class="flex-item" style="padding-top: 16rpx;">规划业绩</span>
            <span>{{item.amount}}元</span>
          </div>
        </div>
      </div>
    </div>
    <div class="mask flex" v-if="isdetail" @tap="hidedetail" style="z-index: 10;">
      <div class="plan_detail" @tap.stop="prevent">
        <div class="plan_detail_title flex">
          <span>规划详情</span>
        </div>
        <div class="plan_detail_body">
          <div v-for="(item,index) in orderlist" :key="index">
            <span>{{item.goodsname}}*{{item.num}}</span>
            <span>{{item.amount}}元</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
import api from '@/utils/api'
import Toast from '@/utils/toast'
export default {
  data () {
    return {
      guest_id: '',
      guest_info: '',
      year: '',
      month: '',
      yearfeat: '',
      planlist: [],
      date: '',
      orderlist: [],
      isdetail: false
    }
  },
  async onLoad () {
    const that = this
    // 接收客户id
    that.guest_id = that.$route.query.id
    // 请求数据
    await Promise.all([
      that.gettoday(),
      that.get_info(),
      that.get_plan_info()
    ])
  },
  components: {
  },
  methods: {
    async gettoday () {
      const that = this
      const date = new Date()
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      that.year = year
      that.month = month
      that.date = year + '-' + month
    },
    async get_info () {
      const that = this
      const res = await api.getguestinfocomme({
        guest_id: that.guest_id
      })
      if (res.code === '100') {
        let data = res.data[0]
        data['arrivetime'] = moment.unix(data['arrivetime']).format('YYYY-MM-DD HH:mm')
        that.guest_info = data
      } else {
        Toast(res.msg, 'noicon')
      }
    },
    async get_plan_info () {
      const that = this
      const res = await api.getguestyearplan({
        guest_id: that.guest_id,
        year: that.year,
        month: that.month
      })
      if (res.code === '100') {
        that.yearfeat = res.year
        const list = res.list
        list.forEach((e, i) => {
          list[i]['arrivetime'] = moment.unix(e['arrivetime']).format('YYYY-MM-DD')
        })
        that.planlist = list
      } else {
        Toast(res.msg, 'noicon')
      }
    },
    setfeat (e) {
      const that = this
      const oldval = e.currentTarget.dataset.oldval
      const value = e.mp.detail.value
      if (oldval != value) { // eslint-disable-line
        that.postplan(value)
      }
    },
    setfeat2 (e) {
      const value = e.mp.detail.value
      if (value > 0) {
        this.postplan(value)
      }
    },
    async postplan (value) {
      const that = this
      const res = await api.postguestplanyear({
        guest_id: that.guest_id,
        year: that.year,
        month: that.month,
        text: value
      })
      if (res.code === '100') {
        that.get_plan_info()
        Toast('修改成功', 'noicon')
      } else {
        Toast('修改失败', 'noicon')
      }
    },
    bindDateChange (e) {
      const that = this
      const date = e.mp.detail.value
      const arr = date.split('-')
      that.year = arr[0]
      that.month = arr[1]
      that.date = date
      that.get_plan_info()
    },
    async showdetail (id) {
      const that = this
      const res = await api.getplanorderinfolist({
        orderid: id
      })
      if (res.code === '100') {
        that.orderlist = res.data
      } else {
        Toast(res.msg, 'noicon')
      }
      that.isdetail = true
    },
    hidedetail () {
      this.isdetail = false
    },
    prevent () { }
  }
}
</script>

<style scoped>
.choose {
  width: 400rpx;
  height: 64rpx;
  text-align: center;
  line-height: 64rpx;
  color: rgba(255, 255, 255, 1);
  font-size: 28rpx;
  border-radius: 40rpx;
  background: rgba(255, 68, 68, 1)
    url("http://mytimg.178mr.com/bottom_arrow.png") no-repeat;
  margin: 30rpx auto;
  background-position: 280rpx 24rpx;
  background-size: 30rpx;
}

.customer {
  display: flex;
  width: 100%;
  padding: 34rpx 0 0 32rpx;
  background-color: rgba(255, 255, 255, 1);
  height: 226rpx;
}
.customer .left {
  margin-right: 40rpx;
}
.customer_pic,
.customer_pic img {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
}
.customer .right span {
  display: block;
  font-size: 28rpx;
  color: rgba(102, 102, 102, 1);
}
.customer_name {
  display: flex !important;
  align-items: center;
  justify-content: space-between;
  padding-right: 28rpx;
}
.customer_name strong {
  font-size: 32rpx !important;
  font-weight: bold;
  color: rgba(51, 51, 51, 1);
}
.customer_shop {
  display: flex !important;
  align-items: center;
}
.customer_shop i {
  width: 24rpx;
  height: 21rpx;
  margin-right: 8rpx;
  background: url(http://mytimg.178mr.com/minishop.png) no-repeat;
  background-size: 100%;
}
.wrap_box {
  width: 100%;
  padding-bottom: 30rpx;
  box-shadow: 0 6rpx 20rpx rgba(255, 68, 68, 0.1);
  background-color: rgba(255, 255, 255, 1);
  margin-top: 18rpx;
}
.wrap_box .title {
  padding: 26rpx 0 8rpx 34rpx;
  font-size: 32rpx;
  font-weight: bold;
  color: rgba(51, 51, 51, 1);
}
.wrap_box .body {
  padding-left: 48rpx;
}
.wrap_box .body > div {
  padding-right: 46rpx;
  font-size: 28rpx;
  color: rgba(102, 102, 102, 1);
  border-bottom: 2rpx solid rgba(153, 153, 153, 0.1);
}
.wrap_box .body > div:last-child {
  border-bottom: none;
}
.plan_customer {
  display: flex;
}
.plan_detail {
  width: 624rpx;
  height: 860rpx;
  border-radius: 24rpx;
  background-color: #fff;
}
.plan_detail_title {
  height: 70rpx;
  color: #ff4444;
  font-size: 32rpx;
  border-bottom: 2rpx solid rgba(153, 153, 153, 0.1);
}
.plan_detail_body {
  padding-left: 48rpx;
}
.plan_detail_body > div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 70rpx;
  font-size: 28rpx;
  color: #666666;
  border-bottom: 2rpx solid rgba(153, 153, 153, 0.1);
}
.plan_detail_body > div:last-child {
  border-bottom: none;
}
.plan_detail_body > div span:last-child {
  color: #999999;
  padding-right: 30rpx;
}
.shu {
  display: inline-block;
  width: 4rpx;
  height: 20rpx;
  background: rgba(255, 68, 68, 1);
  border-radius: 2rpx;
  margin-right: 12rpx;
}
.none {
  text-align: center;
  font-size: 30rpx;
  color: rgba(153, 153, 153, 1);
}
input {
  text-align: right;
}
</style>
